﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Instructor.Models;
using PagedList;


namespace OT.Areas.Instructor.Controllers
{
    public class AccountController : Controller
    {
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        int LanguageID = Utilities.GetLanguageID();
        public ActionResult Index()
        {
            int UserID = 0;
            if (Request.Cookies["LoginSesID"] != null)
            {
                UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
            }
            else
            {
                TempData["notice"] = "Time Out ,Please Login";
                Response.Redirect("~/Home");
            }
            Users Users = null;
            try
            {
                var User = OTDb.Users.Where(r => r.UserID == UserID).SingleOrDefault();
                if (User != null)
                {
                    Users = new Users();
                    Users.UserID = User.UserID;
                    Users.FirstName = User.FirstName;
                    Users.LastName = User.LastName;
                    Users.EmailAddress = User.EmailAddress;
                    Users.Address = User.Address;
                    Users.MobileNo = User.MobileNo;
                    Users.LandlineNo = User.LandlineNo;
                    Users.CreatedDate = (DateTime)User.CreatedDate;
                    Users.UpdatedDate = (DateTime)User.UpdatedDate;
                    Users.LastLoginDate = (DateTime)User.LastLoginDate;
                    Users.CountryID = (int)User.CountryID;
                    var category = OTDb.Countries.ToList();
                    Users.Countrys = category;
                    Users.StateID = (int)User.StateID;
                    var State = OTDb.States.Where(s=>s.CountryID==(int)User.CountryID).ToList();
                    Users.States = State;
                    Users.CityID = (int)User.CityID;
                    var City = OTDb.Cities.Where(c=>c.StateID==(int)User.StateID).ToList();
                    Users.Citys = City;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return View(Users);
        }
        [HttpPost]
        public string Change(string FirstName, string LastName, string Address, string MobileNo, string LandlineNo, int CountryID, int StateID, int CityID)
        {
            string result = "";
            int UserID = 0;
            if (Request.Cookies["LoginSesID"] != null)
            {
                UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
            }
            else
            {
                Response.Redirect("~/Home");
            }
            var User = OTDb.Users.Where(r => r.UserID == UserID).SingleOrDefault();
            if (User != null)
            {
                User.FirstName = FirstName;
                User.LastName = LastName;
                User.Address = Address;
                User.MobileNo = MobileNo;
                User.LandlineNo = LandlineNo;
                User.UpdatedDate = DateTime.Now;
                User.CountryID = (int)CountryID;
                User.StateID = (int)StateID;
                User.CityID = (int)CityID;
                OTDb.SaveChanges();
                result = "valid";
                TempData["notice"] = "Successfully Update";
            }
            return result;
        }
        [HttpPost]
        public ActionResult Index( Users Users)
        {
            try
            {
                int UserID =Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
                if (ModelState.IsValid)
                {
                    var User = OTDb.Users.Where(r => r.UserID == UserID).SingleOrDefault();
                    if (User != null)
                    {
                        var Country = OTDb.Countries.ToList();
                        Users.Countrys = Country;
                        var Stats = OTDb.States.ToList();
                        Users.States = Stats;
                        var City = OTDb.Cities.ToList();
                        Users.Citys = City;
                        User.FirstName = Users.FirstName;
                        User.LastName = Users.LastName;
                        User.EmailAddress = Users.EmailAddress;
                        User.Address = Users.Address;
                        User.MobileNo = Users.MobileNo;
                        User.LandlineNo = Users.LandlineNo;
                        User.UpdatedDate = DateTime.Now;
                        User.CountryID = (int)Users.CountryID;
                        User.StateID = (int)Users.StateID;
                        User.CityID = (int)Users.CityID;
                        OTDb.SaveChanges();
                        TempData["notice"] = "Successfully Update";
                        return RedirectToAction("Index","Account");
                    }
                }
                else
                {

                    Users = new Users();
                    var Country = OTDb.Countries.ToList();
                    Users.Countrys = Country;
                    var Stats = OTDb.States.ToList();
                    Users.States = Stats;
                    var City = OTDb.Cities.ToList();
                    Users.Citys = City;
                }

            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return View(Users);
        }
    }
}
